<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace Common\LibTag;
use Think\Template\TagLib;
/**
 * Xw自定义标签库解析类
 */
class Xw extends TagLib {
    // 标签定义
    protected $tags   =  array(
		// 标签定义： attr 属性列表 close 是否闭合（0 或者1 默认1）alias 标签别名 level 嵌套层次
		//广告
		'ad'          => array('attr'  =>'pid,orderby,limit,return','close'=>1,'level'=>2),
		//栏目
        'catelist'    => array('attr'  => 'cateid,type,return','close' => 1),
        //列表
        'list'        => array('attr'  => 'name,cateid,push,orderby,limit,return','close' => 1),
        //文章
        'article'     => array('attr'  => 'id,cateid,return','close' => 1),
        //上一篇
        'prev'        => array('attr'  => 'cateid,pathname,return','close' => 1),
        //下一篇
        'next'        => array('attr'  => 'cateid,pathname,return','close' => 1),
        //面包屑标签
        'position'    => array('attr'  => 'cateid,pathname,return','close' => 1),
    );
    //文章
    public function _article($attr, $content)
    {
       	$id    	   = !isset($attr['id']) || $attr['id'] == '' ? intval (I('get.id')) : trim($attr['id']); //只接收一个文章ID
       	$cateid    = !isset($attr['cateid']) || $attr['cateid'] == '' ? 0 : trim($attr['cateid']); 
       	if($cateid==0 && I('get.pathname')){
       		$pathname =I('get.pathname');
       	}
       	//只接收一个栏目ID
        $return  = empty($attr['return']) ? 'list' : $attr['return'];
        $str = <<<str
		<?php
			\$_id = intval($id);
			\$_cateid = intval($cateid);
			\$_pathname = "$pathname";
			if(\$_cateid==0){
				\$w = array('pathname'=> \$_pathname);
			}else{
				\$w = array('cateid'=> \$_cateid);
			}
			\$where = array('id'=> \$_id);
			\$model=M('category')->where(\$w)->find();
			\$modelid=\$model['modelid'];
			\$table=M('model')->where("modelid=\$modelid")->find();
			\$name="model_".\$table['table'];
			\$_list=M("\$name")->where(\$where)->select();
			dump(\$_list);
			foreach(\$_list as \$k=>\$v){
				foreach(\$v as \$k1=>\$v1){
					\$formtype = M('model_field')->where('modelid='.\$modelid.' AND field="'.\$k1.'"')->getField('formtype');
					switch (\$formtype) {
						case 'editor':
							\$_list[\$k][\$k1] = html_entity_decode(\$_list[\$k][\$k1]);
							break;
						case 'image':
							\$image=intval(\$_list[\$k][\$k1]);
							\$url= M('files')->where("id=\$image")->getField('path');
							\$_list[\$k][\$k1]=\$url;
							break;
						case 'images':
							\$images=\$_list[\$k][\$k1];
							\$img_arr = explode(',',\$images);
							unset(\$_list[\$k][\$k1]);
							foreach(\$img_arr as \$k2=>\$v2){
								\$url= M('files')->where("id=\$v2")->getField('path');
								\$_list[\$k][\$k1][]=\$url;
							}
							break;
						default:
							break;
					}
				}
			}
			if (empty(\$_list)) {
				\$_list = array();
			}
			dump(\$_list);
			foreach(\$_list as \$autoindex => \$$return):
		?>
str;
        $str .= $content;
        $str .= '<?php endforeach;?>';
        return $str;

    }
    //列表
    public function _list($attr, $content)
    {
       	$id    	  = !isset($attr['cateid']) || $attr['cateid'] == '' ? intval (I('get.cateid')) : trim($attr['cateid']); 
       	//只接收一个栏目ID
        $orderby  = empty($attr['orderby']) ? 'id ASC' : $attr['orderby'];
        $limit    = empty($attr['limit']) ? '10' : $attr['limit'];
        $push     = empty($attr['push']) ? '0' : $attr['push'];
        $pagesize = M("category")->where("cateid=$id")->getField('pagesize');
        $ipage=I('get.page');
        $page     = empty($ipage) ? "1" : intval($ipage);
        $ispage   = empty($attr['ispage']) ? intval(I('get.ispage')) : $attr['ispage'];
        $return   = empty($attr['return']) ? 'list' : $attr['return'];
        $str = <<<str
		<?php
			\$_id = intval($id);
			\$_push = "$push";
			\$_pagesize = intval($pagesize);
			\$_page=intval($page);
			\$_ispage=intval($ispage);
			\$w = array('cateid'=> \$_id);
			if(empty(\$_push)){
				\$where = array('catid'=> \$_id);
			}else{
				\$where = array('catid'=> \$_id, 'push' => \$_push);
			}
			\$model=M('category')->where(\$w)->find();
			
			if(\$model['is_final']=='1'){
				\$modelid=\$model['modelid'];
				\$table=M('model')->where("modelid=\$modelid")->find();
				\$name="model_".\$table['table'];
				//分页
				\$count=M("\$name")->where(\$where)->count();
				\$pages=new Common\Lib\Page(\$count,\$_pagesize) ;
				//分页设置
		        \$pages->setConfig('prev','上一页');
		        \$pages->setConfig('next','下一页');
		        \$pages->setConfig('first','首页');
		        \$pages->setConfig('last','末页');
		        \$pages->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
				if (\$_ispage > 0) {
					\$limit = \$_pagesize*(\$_page-1). ',' .\$_pagesize;
					\$page = \$pages->show(\$model['cate_url']);
				}else{
					\$limit = "$limit";
				}
				\$_list = M("\$name")->where(\$where)->order('$orderby')->limit(\$limit)->select();
				if(\$_list){
					foreach(\$_list as \$k => \$v){
						\$fileid=\$v['thumb'];
						\$url= M('files')->where("id=\$fileid")->getField('path');
						\$_list[\$k]['thumb']=\$url;
					}
				}
				\$_list=Common\Lib\Category::url(\$_list);	
			}else{
				// 判断栏目是否是终极栏目，不是则显示子栏目，是则显示信息
				\$_list=M("category")->where("parentid=\$_id")->select();
				\$_list=Common\Lib\Category::make_url(\$_list);	
			}
			if (empty(\$_list)) {
				\$_list = array();
			}
			foreach(\$_list as \$autoindex => \$$return):
				
		?>
str;
        $str .= $content;
        $str .= '<?php endforeach;?>';
        return $str;

    }

    //栏目列表
    public function _catelist($attr, $content)
    {
       	$id      = !isset($attr['cateid']) || $attr['cateid'] == '' ? 0 : trim($attr['cateid']);
       	$type    = empty($attr['type']) ? 0 : intval($attr['type']);
       	$return  = empty($attr['return']) ? 'list' : $attr['return'];
        $str     = <<<str
		<?php
			\$_id = intval($id);			
			\$where = array('cateid'=> \$_id, 'is_nav' => 1);
			if($type > 0){
				\$catelist = M('category')->where(\$where)->select();
			}else{
				\$list = M('category')->select();
				\$_list = M('category')->where(\$where)->select();
				\$_catlist = Common\Lib\Category::subtree(\$list,\$_id);
				\$cate=array_merge(\$_list,\$_catlist);
				\$catelist=Common\Lib\Category::make_tree(\$cate);
			}
			if(\$_id==0){
				\$list = M('category')->where("is_nav=1")->select();
				\$cat=Common\Lib\Category::make_url(\$list);
				
				\$catelist=Common\Lib\Category::make_tree(\$cat);
			}
			if (empty(\$catelist)) {
				\$catelist = array();
			}
			foreach(\$catelist as \$auto => \$$return):
				 
		?>
str;
        $str .= $content;
        $str .= '<?php endforeach;?>';
        return $str;
    }
    //广告列表
	public function _ad ($attr,$content){
		$id       = !isset($attr['pid']) || $attr['pid'] == '' ? 0 : trim($attr['pid']);
		$orderby  = empty($attr['orderby']) ? 'adid ASC' : $attr['orderby'];
        $limit    = empty($attr['limit']) ? '10' : $attr['limit'];
        $return   = empty($attr['return']) ? 'list' : $attr['return'];
        $str      = <<<str
		<?php
			\$_id = intval($id);
			\$where = array('pid'=> \$_id, 'status' => 1);
			\$limit = "$limit";
			\$ad_list = M('adp')->find(\$_id);
			if (\$ad_list) {
				\$_ad = M('ad')->where(\$where)->order('$orderby')->limit(\$limit)->select();
			}
			if(\$_ad){
				foreach(\$_ad as \$k => \$v){
					\$fileid=\$v['fileid'];
					\$url= M('files')->where("id=\$fileid")->getField('path');
					\$_ad[\$k]['imgurl']=\$url;
				}
			}				
			if (empty(\$_ad)) {
				\$_ad = array();
			}
			if (empty(\$url)) {
				\$url = array();
			}
			foreach(\$_ad as \$autoindex => \$$return):
				\${$return['width']} = \$ad_list['width'];
				\${$return['height']} = \$ad_list['height'];
		?>
str;
        $str .= $content;
        $str .= '<?php endforeach;?>';
        return $str;
	}

	//上一篇
	public function _prev($attr, $content){
		$id       = !isset($attr['id']) || $attr['id'] == '' ? intval (I('get.id')) : trim($attr['id']);
		$pathname = !isset($attr['pathname']) || $attr['pathname'] == '' ? I('get.pathname') : trim($attr['pathname']);
        $str      = <<<str
<?php
		\$_id = intval($id);
		\$_pathname = "$pathname";
		\$cateid=M("category")->where("pathname='".\$_pathname."'")->getField("cateid");
		\$modelid=M("category")->where("pathname='".\$_pathname."'")->getField("modelid");
		\$tablename=M("model")->where("modelid=\$modelid")->getField("table");
		\$name="model_".\$table['table'];
		\$previd=\$_id-1;
		\$where = array('id'=> \$previd, 'catid' => \$cateid);
		\$_list=M("\$name")->where(\$where)->select();
		if(\$_list){
			//改变路径
			\$_list=Common\Lib\Category::url(\$_list);
			foreach(\$_list as \$k=>\$v){
			\$catelist=\$v;
			}
			echo '<a href="'. \$catelist['url'] .'" target>'. \$catelist['title'] .'</a>';
		}else{
			echo "第一篇";
		}
?>
str;
        return $str;
    }

    //下一篇
	public function _next($attr, $content){
		$id       = !isset($attr['id']) || $attr['id'] == '' ? intval (I('get.id')) : trim($attr['id']);
		$pathname = !isset($attr['pathname']) || $attr['pathname'] == '' ? I('get.pathname') : trim($attr['pathname']);
        $str      = <<<str
<?php
		\$_id = intval($id);
		\$_pathname = "$pathname";
		\$cateid=M("category")->where("pathname='".\$_pathname."'")->getField("cateid");
		\$modelid=M("category")->where("pathname='".\$_pathname."'")->getField("modelid");
		\$tablename=M("model")->where("modelid=\$modelid")->getField("table");
		\$name="model_".\$table['table'];
		\$nextid=\$_id+1;
		\$where = array('id'=> \$nextid, 'catid' => \$cateid);
		\$_list=M("\$name")->where(\$where)->select();
		if(\$_list){
			//改变路径
			\$_list=Common\Lib\Category::url(\$_list);
			foreach(\$_list as \$k=>\$v){
			\$catelist=\$v;
			}
			echo '<a href="'. \$catelist['url'] .'" target>'. \$catelist['title'] .'</a>';
		}else{
			echo "最后一篇";
		}
?>
str;
        return $str;
    }

    //面包屑标签
	public function _position($attr, $content){
		$id       = !isset($attr['id']) || $attr['id'] == '' ? intval (I('get.id')) : trim($attr['id']);
		$pathname = !isset($attr['pathname']) || $attr['pathname'] == '' ? I('get.pathname') : trim($attr['pathname']);
		$m = MODULE_NAME;
		if($m == 'Home' || $m == 'Admin'){
			//静态地址
			$u="/www";
		}else{
			//静态地址
			$u="/m";
		}
        $str      = <<<str
<?php
		\$_id = intval($id);
		\$_pathname = "$pathname";
		\$cateid=M("category")->where("pathname='".\$_pathname."'")->getField("cateid");
		//当前栏目
		\$catename=M("category")->where("pathname='".\$_pathname."'")->getField("catename");
		
		// 查询父级栏目
		\$cate=M("category")->select();
		\$_list=Common\Lib\Category::getParents(\$cate,\$cateid);
		if(\$_list){
			\$modelid=M("category")->where("pathname='".\$_pathname."'")->getField("modelid");
			\$tablename=M("model")->where("modelid=\$modelid")->getField("table");
			\$name="model_".\$tablename;
			\$where = array('id' => \$_id);
			\$info=M("\$name")->where(\$where)->find();
			\$title=\$info['title'];
			\$_list=implode(' > ',\$_list);
			if(\$title){
				echo "<a href='$u/index.shtml'>首页</a><span>&gt;</span>\$_list > \$title";
			}else{
				echo "<a href='$u/index.shtml'>首页</a><span>&gt;</span>\$_list";
			}
		}else{
			echo "首页<span>&gt;</span>";
		}
		
		
?>
str;
        return $str;
    }

     

}  
	

